<?php
/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/5/22
 * Time: 下午7:38
 */

namespace app\index\model;


use think\Model;

class Company extends Model
{
    protected $table = 't_company';
    public function subcategorys()
    {
        return $this->belongsToMany('SubCategory','t_categorycompany',"category_id","company_id");
    }
    public static function getSeoInfoById($id){
        $company = self::find($id);
        $seo['title'] = $company['name'];
        $seo['desc'] = $company['desc'];
        return $seo;

    }
    public static function getRecList(){
        $data = self::order("show_order asc")->limit(0,4)->select();
        if($data->isEmpty()){
            return[];
        }
        $rec_list = [];
        foreach ($data as $item){

            $rec_item['id'] = $item["id"];
            $rec_item['name'] = $item["name"];
            $rec_item['star'] = $item["star"];
            $rec_item['view_num'] = $item["view_num"];
            $rec_item['logo'] = config("app.img_prefix").$item["logo"];
            $rec_item['logo_watermark'] = config("app.img_prefix").$item["logo"];
            array_push($rec_list,$rec_item);

        }
        return $rec_list;

    }
    public static function getList($page){
        $page_size = 10;
        $data = self::where("flag","=",3)->order('show_order','asc')->paginate($page_size,false,['page' => $page]);
        $companyList =[];
        foreach ($data as &$item){
            $item["logo"] = config('app.img_prefix').$item['logo'];
            $item["province_id"] = Dist::getDistNameById($item["province_id"]);
            $item["city_id"] = Dist::getDistNameById($item["city_id"]);
            $item["address"] = $item["province_id"].$item["city_id"].$item["address"];
            $subcateroys = $item->subcategorys;
            $category = " ";
            if($subcateroys != null){
                foreach ($subcateroys as &$cate){
                    $category .= $cate["category_name"]."-";
                }
                $category = substr($category,1,strlen($category)-1);
            }

            $item["categorys"] = $category;
            unset($item['subcategorys']);
            unset($item["province_id"]);
            unset($item["city_id"]);
            $companyList[] = $item;
        }
        $total = $data->total();
        $pageNum = ceil($total/$page_size);
        $currentPage = $data->currentPage();
        $page_arr = [];
        for ($i = 1;$i <=$pageNum;$i++){
            $page_arr[] = $i;
        }
        $ret = [
            "totalNum"=>$total,
            "page_arr" =>$page_arr,
            "totalPage"=>$pageNum,
            "currentPage"=>$currentPage,
            "list"=>$companyList
        ];

        return $ret;
    }
    public static function getOne($id){
        $data = self::find($id);
        $hearinfo['company_name'] = $data['name'];
        $hearinfo['logo'] = config('app.img_prefix').$data['logo'];
        $hearinfo['star'] = $data['star'];
        $hearinfo['view_num'] = $data['view_num'];
        $province = Level::getLevelNameById($data["province_id"]);
        $city = Level::getLevelNameById($data["city_id"]);
        $hearinfo["address"] = $province.$city.$data["address"];

        $ablum = $data['pictures'];
        $ablum = json_decode($ablum);
        if($ablum){
            foreach ($ablum as &$item){
                $item = config('app.img_prefix').$item;
            }
        }


        $category = " ";
        $subcateroys = $data->subcategorys;
        $categoryList = [];
        if($subcateroys != null){
            foreach ($subcateroys as &$cate){
                array_push($categoryList,$cate["category_name"]);
            }

        }


        $ret['id'] =$data['id'];


        $ret['hearinfo'] =$hearinfo;

        $ret['album'] =$ablum;
        $ret['desc'] =$data['desc'];

        $ret['company_desc'] =$data['desc'];
        $ret['licence_poster'] = config('app.img_prefix').$data['licence_poster'];
        $ret['contact_name'] =$data['contact_name'];
        $ret['contact_number'] =$data['contact_number'];

        $ret['scale'] =$data['scale'];





        $ret['hearinfo'] =$hearinfo;

        $ret['scale'] =$data['scale'];


        $ret['categorys'] =$categoryList;



        $ret['advantage'] = $data['advantage'];

        $ret['company_case'] = CompanyCase::getCompanyCaseById($id);
        $ret['company_staff'] = CompanyStaff::getCompanyStaff($id);
        return $ret;
    }
}